<template>
	<xn-form-container
		:title="formData.id ? '编辑排课' : '增加排课'"
		:width="700"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="教室：" name="roomId">
				<a-select
					v-model:value="formData.roomId"
					show-search
					:options="roomData"
					:filter-option="filterOption"
					:field-names="{ label: 'name', value: 'id' }"
					style="width: 100%"
					placeholder="请选择教室"
					allow-clear
				>
				</a-select>
			</a-form-item>
			<a-form-item label="时段：" name="timeslotId">
				<a-select
					v-model:value="formData.timeslotId"
					show-search
					:options="timeslotData"
					:filter-option="filterOption"
					:field-names="{ label: 'name', value: 'id' }"
					style="width: 100%"
					placeholder="请选择时段"
					allow-clear
				>
				</a-select>
			</a-form-item>
			<a-form-item label="长短课时：" name="extended">
				<a-radio-group
					v-model:value="formData.extended"
					:options="extendedOptions"
					placeholder="请选择"
				></a-radio-group>
			</a-form-item>
			<a-form-item label="开始周：" name="startWeek">
				<a-input-number v-model:value="formData.startWeek" style="width: 100%" placeholder="请输入开始周" allow-clear />
			</a-form-item>
			<a-form-item label="结束周：" name="endWeek">
				<a-input-number v-model:value="formData.endWeek" style="width: 100%" placeholder="请输入结束周" allow-clear />
			</a-form-item>
			<a-form-item label="开课：" v-if="!formData.lectureId" name="lectureId">
				<a-input v-model:value="formData.lectureId" placeholder="请输入开课ID" allow-clear />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="eduLessonForm">
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import eduLessonApi from '@/api/biz/eduLessonApi'
	import eduRoomApi from '@/api/biz/eduRoomApi'
	import eduTimeslotApi from '@/api/biz/eduTimeslotApi'
	import tool from "@/utils/tool";
	// 抽屉状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	// 教室数据
	let roomData = ref([])
	// 时段数据
	let timeslotData = ref([])
	// 长短课时
	const extendedOptions = tool.dictList('EXTENDED_LESSON')

	// 打开抽屉
	const onOpen = (record, lectureId) => {
		visible.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
		if (lectureId) {
			formData.value.lectureId = lectureId
		}
		seleRoomData()
		seleTimeslotData()
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		visible.value = false
		roomData.value = []
		timeslotData.value = []
	}
	// 默认要校验的
	const formRules = {
		extended: [required('请选择单次授课连续课时数')],
	}
	// 选择教室
	const seleRoomData = () => {
		const param = {}
		eduRoomApi.eduRoomList(param).then((res) => {
			roomData.value = res
		})
	}
	// 选择时段
	const seleTimeslotData = () => {
		const param = {}
		eduTimeslotApi.eduTimeslotList(param).then((res) => {
			timeslotData.value = res
		})
	}
	// 过滤选项
    const filterOption = (input, option) => {
		return option.name.toLowerCase().indexOf(input.toLowerCase()) >= 0;
    }
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			eduLessonApi
				.eduLessonSubmitForm(formDataParam, !formDataParam.id)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
